#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
  
  int main()
  {
	pid_t id = fork();
	if(id == -1)
	{
		printf("创建子进程失败\n");
	}else if(id == 0)
	{
		int i = 5;
		while(i--)
		{
			printf("我是子进程，pid:%d，需要运行五次，已经运行%d次\n",getpid(),5-i);
			sleep(3);
		}
	}
	else
	{
		int status = 0;
		pid_t ret = waitpid(id,&status,0);
		if(ret > 0)
		{
			if(WIFEXITED(status))
			{
				printf("子进程正常返回：pid:%d，进程退出码:%d\n",ret,WEXITSTATUS(status));
			}
			else
			{
				printf("子进程异常终止返回：pid:%d，%d号信号\n",ret,status&0x7f);
			}
		}
		else
		{
			printf("等待子进程失败\n");
		}
	}
	
	return 0;
 }
